# -*- coding: utf-8 -*-
"""
    xfree.auth.forms
    ~~~~~~~~~~~~~~~~~~~~~
    文件描述：这个模块提供了认证视图所需的表单。
    作者：xfree
    邮箱：cy_83k@qq.com

"""

import logging

# 用于延迟获取翻译文本（懒加载）
from flask_babelplus import lazy_gettext as _
from wtforms import (
    BooleanField,  # 布尔字段
    HiddenField,  # 隐藏字段
    PasswordField,  # 密码字段
    SelectField,  # 选择字段
    StringField,  # 字符串字段
    SubmitField,  # 提交按钮字段
)
from wtforms.validators import (
    DataRequired,  # 数据必填验证器
    Email,  # 邮箱格式验证器
    EqualTo,  # 字段值相等验证器
    InputRequired,  # 输入必填验证器
    regexp,  # 正则表达式验证器
)

# 导入自定义的RecaptchaField
from xfree.utils.fields import RecaptchaField
from xfree.utils.forms import XFreeForm

# 获取日志记录器
logger = logging.getLogger(__name__)

# 定义用户名的正则表达式
USERNAME_RE = r"^[\w.+-]+$"
# 创建一个基于正则表达式的用户名验证函数
is_valid_username = regexp(
    USERNAME_RE, message=_("You can only use letters, numbers or dashes.")
)


class LoginForm(XFreeForm):
    """
    登录表单类
    """
    # 用户名或邮箱地址字段
    login = StringField(
        _("Username or Email address"),
        validators=[
            DataRequired(message=_("Please enter your username or email address."))
        ],
    )
    # 密码字段
    password = PasswordField(
        _("Password"),
        validators=[DataRequired(message=_("Please enter your password."))],
    )
    # 记住我复选框字段
    remember_me = BooleanField(_("Remember me"), default=False)
    # 提交按钮字段
    submit = SubmitField(_("Login"))
    # 隐藏的验证码字段（初始版本）
    recaptcha = HiddenField(_("Captcha"))


class LoginRecaptchaForm(LoginForm):
    """
    带有reCAPTCHA验证的登录表单类，继承自LoginForm
    """
    # 使用RecaptchaField替换隐藏的验证码字段
    recaptcha = RecaptchaField(_("Captcha"))


class RegisterForm(XFreeForm):
    """
    注册表单类
    """
    # 用户名字段
    username = StringField(
        _("Username"),
        validators=[
            DataRequired(message=_("A valid username is required")),
            is_valid_username,
        ],
    )
    # 邮箱地址字段
    email = StringField(
        _("Email address"),
        validators=[
            DataRequired(message=_("A valid email address is required.")),
            Email(message=_("Invalid email address.")),
        ],
    )
    # 密码字段
    password = PasswordField(
        _("Password"),
        validators=[
            InputRequired(),
            EqualTo("confirm_password", message=_("Passwords must match.")),
        ],
    )
    # 确认密码字段
    confirm_password = PasswordField(_("Confirm password"))
    # reCAPTCHA字段
    recaptcha = RecaptchaField(_("Captcha"))
    # 语言选择字段
    language = SelectField(_("Language"))
    # 接受服务条款复选框字段
    accept_tos = BooleanField(
        _("I accept the Terms of Service"),
        validators=[DataRequired(message=_("Please accept the TOS."))],
        default=True,
    )
    # 提交按钮字段
    submit = SubmitField(_("Register"))


class ReauthForm(XFreeForm):
    """
    重新认证表单类
    """
    # 密码字段
    password = PasswordField(
        _("Password"),
        validators=[DataRequired(message=_("Please enter your password."))],
    )
    # 提交按钮字段
    submit = SubmitField(_("Refresh Login"))


class ForgotPasswordForm(XFreeForm):
    """
    忘记密码表单类
    """
    # 邮箱地址字段
    email = StringField(
        _("Email address"),
        validators=[
            DataRequired(message=_("A valid email address is required.")),
            Email(),
        ],
    )
    # reCAPTCHA字段
    recaptcha = RecaptchaField(_("Captcha"))
    # 提交按钮字段
    submit = SubmitField(_("Request Password"))


class ResetPasswordForm(XFreeForm):
    """
    重置密码表单类
    """
    # 令牌隐藏字段
    token = HiddenField("Token")
    # 邮箱地址字段
    email = StringField(
        _("Email address"),
        validators=[
            DataRequired(message=_("A valid email address is required.")),
            Email(),
        ],
    )
    # 密码字段
    password = PasswordField(
        _("Password"),
        validators=[
            InputRequired(),
            EqualTo("confirm_password", message=_("Passwords must match.")),
        ],
    )
    # 确认密码字段
    confirm_password = PasswordField(_("Confirm password"))
    # 提交按钮字段
    submit = SubmitField(_("Reset password"))


class RequestActivationForm(XFreeForm):
    """
    请求激活表单类
    """
    # 用户名字段
    username = StringField(
        _("Username"),
        validators=[
            DataRequired(message=_("A valid username is required.")),
            is_valid_username,
        ],
    )
    # 邮箱地址字段
    email = StringField(
        _("Email address"),
        validators=[
            DataRequired(message=_("A valid email address is required.")),
            Email(message=_("Invalid email address.")),
        ],
    )
    # 提交按钮字段
    submit = SubmitField(_("Send Confirmation Mail"))


class AccountActivationForm(XFreeForm):
    """
    账户激活表单类
    """
    # 令牌字符串字段
    token = StringField(
        _("Email confirmation token"),
        validators=[DataRequired(_("Please enter the token we have sent to you."))],
    )
    # 提交按钮字段
    submit = SubmitField(_("Confirm Email"))